<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .date {
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div v-outside class="box">
            <input type="text">
            <div class="date" v-if="isShow">
                日历控件
                <button>选择日期</button>
            </div>
        </div>
    </div>
</body>

</html>
<script src="vue.js"></script>
<script>
    //若点击的位置在box里面,date这个div显示，若不在里面，则date这个div隐藏
    let vm = new Vue({
        el: '#app',
        data: {
            isShow: false
        },
        methods: {
            blur() {
                this.isShow = false;
            },
            focus() {
                this.isShow = true;
            }
        },
        directives: {
            "outside": {
                bind(el, bindings, vnode) {
                    //context 指上下文，也就时vue实例
                    el.fn = (e) => {
                        //e.target事件源
                        //    console.log(el.contains(e.target));
                        if (el.contains(e.target)) {
                            vnode.context.focus()
                        } else {
                            vnode.context.blur();
                        }
                    }
                    document.addEventListener('click', el.fn, false)
                },
                unbind(el) {
                    el.removeListener('click', el.fn);
                }
            }
        }
    })
</script>